#include "junix.h"

int jconnect_un(char *path)
{
	size_t len;
	int fd;

	struct sockaddr_un sun;
	sun.sun_family = AF_UNIX;
	strncpy( sun.sun_path, path, 108 );
	len=sizeof(sun.sun_family)+strlen(sun.sun_path);
	fd=Socket(AF_UNIX,SOCK_STREAM,0);
	Connect(fd,&sun,len);

	return fd;
}

int jpassive_un(char *path)
{
	size_t len;
	int fd;

	struct sockaddr_un sun;
	sun.sun_family = AF_UNIX;
	strncpy( sun.sun_path, path, 108 );
	len=sizeof(sun.sun_family)+strlen(sun.sun_path);
	unlink(path);
	fd=Socket(AF_UNIX,SOCK_STREAM,0);
	Bind(fd,&sun,len);
	Listen(fd,0);

	return fd;
}

int main(int argc, char **argv)
{
	pid_t pid=Fork();
	if(!pid)
	{
		sleep(4);
		int fd=connect_un("hello");
		Write(fd,"Hello",5);

	}else{

		int fd,connfd;
		char buf[BUFSIZ];
		ssize_t len;

		fd=passive_un("hello");
		while(1)
		{
			connfd=Accept(fd,NULL,NULL);
			while( (len=Read(connfd,buf,BUFSIZ)) > 0 )
				Write(STDOUT_FILENO,buf,len);

			Close(connfd);
		}
	}

	exit(0);
}

